/*
 * ! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.core.database.dialog;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.RowMetaAndData;
import org.pentaho.di.core.database.DatabaseInterface;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.core.database.dialog.DatabaseExplorerDialog;
import org.pentaho.di.ui.core.dialog.ErrorDialog;
import org.pentaho.di.ui.core.dialog.PreviewRowsDialog;
import org.pentaho.ui.database.DatabaseConnectionListItem;
import org.pentaho.ui.database.event.DataHandler;
import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.containers.XulListbox;
import org.pentaho.ui.xul.containers.XulTree;
import org.yzbdl.lanius.util.LogUtil;

/**
 * @author yzbdl@yzbdl.ac.cn
 * @version: 1.0
 * @Date 2020/08/01 18:53
 **/
public class DataOverrideHandler extends DataHandler {
    boolean cancelPressed = false;
    private XulListbox connectionBox;
    private XulListbox accessBox;

    /**
     * 商店插件服务器地址
     */
    @Override
    public void onCancel() {
        super.onCancel();
        this.cancelPressed = true;
    }

    @Override
    public void onOK() {
        super.onOK();
        this.cancelPressed = false;
    }

    @Override
    public Object getData() {
        if (!cancelPressed) {
            return super.getData();
        } else {
            return null;
        }
    }

    private static Class<?> PKG = DataOverrideHandler.class; // for i18n purposes, needed by Translator2!!

    private java.util.List<DatabaseMeta> databases;

    public DataOverrideHandler() {}

    public void explore() {

        Shell parent = getShell();

        DatabaseMeta dbinfo = new DatabaseMeta();
        getInfo(dbinfo);

        try {
            if (dbinfo.getAccessType() != DatabaseMeta.TYPE_ACCESS_PLUGIN) {
                DatabaseExplorerDialog ded = new DatabaseExplorerDialog(parent, SWT.NONE, dbinfo, databases, true);
                ded.open();
            } else {
                MessageBox mb = new MessageBox(parent, SWT.OK | SWT.ICON_INFORMATION);
                mb.setText(BaseMessages.getString(PKG, "DatabaseDialog.ExplorerNotImplemented.Title"));
                mb.setMessage(BaseMessages.getString(PKG, "DatabaseDialog.ExplorerNotImplemented.Message"));
                mb.open();
            }
        } catch (Exception e) {
            new ErrorDialog(parent, BaseMessages.getString(PKG, "DatabaseDialog.ErrorParameters.title"),
                BaseMessages.getString(PKG, "DatabaseDialog.ErrorParameters.description"), e);
        }
    }

    private Shell getShell() {
        Object obj = document.getRootElement().getManagedObject();
        Shell parent;
        if (obj instanceof Shell) {
            parent = (Shell)obj;
        } else {
            parent = ((Composite)obj).getShell();
        }
        if (parent == null) {
            throw new IllegalStateException("Could not get Shell reference from Xul Dialog Tree.");
        }
        return parent;
    }

    public DataOverrideHandler getDataOverrideHandler() {
        return this;
    }

    public void showFeatureList() {

        Shell parent = getShell();

        DatabaseMeta dbinfo = new DatabaseMeta();
        getInfo(dbinfo);

        try {
            java.util.List<RowMetaAndData> buffer = dbinfo.getFeatureSummary();
            if (buffer.size() > 0) {
                RowMetaInterface rowMeta = buffer.get(0).getRowMeta();
                java.util.List<Object[]> rowData = new ArrayList<Object[]>();
                for (RowMetaAndData row : buffer) {
                    rowData.add(row.getData());
                }

                PreviewRowsDialog prd = new PreviewRowsDialog(parent, dbinfo, SWT.NONE, null, rowMeta, rowData);
                prd.setTitleMessage(BaseMessages.getString(PKG, "DatabaseDialog.FeatureList.title"),
                    BaseMessages.getString(PKG, "DatabaseDialog.FeatureList.title"));
                prd.open();
            }
        } catch (Exception e) {
            new ErrorDialog(parent, BaseMessages.getString(PKG, "DatabaseDialog.FeatureListError.title"),
                BaseMessages.getString(PKG, "DatabaseDialog.FeatureListError.description"), e);
        }
    }

    @Override
    public void getControls() {

        super.getControls();
        connectionBox = (XulListbox)document.getElementById("connection-type-list");
        accessBox = (XulListbox)document.getElementById("access-type-list");

        DatabaseConnectionListItem connListItem = (DatabaseConnectionListItem)connectionBox.getSelectedItem();
        // 给一个默认值
        if (connListItem == null) {
            // 查找第一个数据库连接，因为存在内置数据库，默认不会为空
            DatabaseInterface defaultDi = DatabaseMeta.getDatabaseInterfaces()[0];
            connListItem = new DatabaseConnectionListItem(defaultDi);
        }
        DatabaseInterface database = connListItem.getDatabase();
        XulComponent[] boxes = new XulComponent[] {hostNameBox, databaseNameBox, portNumberBox, userNameBox,
            passwordBox, customDriverClassBox, customUrlBox, dataTablespaceBox, indexTablespaceBox, poolSizeBox,
            maxPoolSizeBox, languageBox, serverInstanceBox, systemNumberBox, clientBox, resultStreamingCursorCheck,
            customDriverCheck, customDriverText, customDriverSelectButton};
        Object accessKey = accessBox.getSelectedItem();
        LogUtil.debug("连接方式:" + accessKey.toString());
        int access = DatabaseMeta.getAccessType((String)accessKey);
        // database.setAccessType(access);
        switch (access) {
            case DatabaseMeta.TYPE_ACCESS_JNDI:
                checkDriver(database, boxes);
                break;
            case DatabaseMeta.TYPE_ACCESS_NATIVE:
                checkDriver(database, boxes);
                break;
            case DatabaseMeta.TYPE_ACCESS_OCI:
                checkDriver(database, boxes);
                break;
            case DatabaseMeta.TYPE_ACCESS_ODBC:
                checkDriver(database, boxes);
                break;
            case DatabaseMeta.TYPE_ACCESS_PLUGIN:
                break;
            default:
                break;
        }

        XulTree[] trees = new XulTree[] {poolParameterTree, clusterParameterTree, optionsParameterTree};

        for (int i = 0; i < trees.length; i++) {
            XulTree xulTree = trees[i];
            if (xulTree != null) {
                xulTree.setData(databaseMeta);
            }
        }
    }

    /**
     * 判断驱动是否存在
     *
     * @param database
     * @param boxes
     */
    public void checkDriver(DatabaseInterface database, XulComponent[] boxes) {
    }

    public java.util.List<DatabaseMeta> getDatabases() {
        return databases;
    }

    public void setDatabases(java.util.List<DatabaseMeta> databases) {
        this.databases = databases;
    }

}
